//
//  PPAbstractScrollablePictureViewController.m
//  pickpic
//
//  Created by Jinxiao on 10/26/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "PPAbstractScrollablePictureViewController.h"

@interface PPAbstractScrollablePictureViewController ()
@property (readwrite, nonatomic, assign) BOOL navigationBarHidden;
@end

@implementation PPAbstractScrollablePictureViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.edgesForExtendedLayout = UIRectEdgeTop;
}

//- (void)viewWillAppear:(BOOL)animated {
//    [super viewWillAppear:animated];
//
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithRGBHex:0x00 alpha:0.5]] forBarMetrics:UIBarMetricsDefault];
//}
//
//- (void)viewWillDisappear:(BOOL)animated {
//    [super viewWillDisappear:animated];
//
//    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
//}

- (void)willBeginScrolling {
    [super willBeginScrolling];

    if([self scrollsOnScrolling]) {
        if(!_navigationBarHidden) {
            _navigationBarHidden = YES;
            
            [self dismissNavigationBarAnimated:YES completion:nil];
        }
    }
}

- (void)displayNavigationBarAnimated:(BOOL)animated completion:(void (^)(void))completion {
    [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
//        [self.navigationController setNavigationBarHidden:NO animated:YES];

        [[self extraViewsToHidden] enumerateObjectsUsingBlock:^(UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            obj.bottom = self.view.height;
        }];
    } completion:^(BOOL finished) {
        !completion ?: completion();
    }];
}

- (void)dismissNavigationBarAnimated:(BOOL)animated completion:(void (^)(void))completion {
    [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
//        [self.navigationController setNavigationBarHidden:YES animated:YES];

        [[self extraViewsToHidden] enumerateObjectsUsingBlock:^(UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            obj.top = self.view.height;
        }];
    } completion:^(BOOL finished) {
        !completion ?: completion();
    }];
}

- (void)pictureViewCell:(PPAbstractPictureViewCell *)pictureViewCell didRecognizedSingleTapGesture:(UITapGestureRecognizer *)gesture {
    _navigationBarHidden = !_navigationBarHidden;

    if(_navigationBarHidden) {
        [self dismissNavigationBarAnimated:YES completion:nil];
    } else {
        [self displayNavigationBarAnimated:YES completion:nil];
    }
}

- (BOOL)scrollsOnScrolling {
    return NO;
}

- (NSArray <UIView *> *)extraViewsToHidden {
    return nil;
}

@end
